Page({
  data: {
    bookList: [],
    imgUrl: "",
    historyArr: wx.getStorageSync('historyArr') || [],
  },
  // 1. 获取书籍列表
  getBookList() {
    wx.showLoading({
      title: "正在加载中...",
    });
    wx.request({
      url: "https://hmajax.itheima.net/api/books",
      data: {
        creator: "zhangsan",
      },
      method: "GET",
      success: (res) => {
        // console.log(result);
        // this.setData({
        //   bookList: res.data.data,
        // });
        setTimeout(() => {
          this.setData({
              bookList: res.data.data,
            },
            () => {
              // console.log("该函数被执行, 意味着数据和视图都更新完毕了");
              wx.showToast({
                title: "查询书单成功!",
                icon: "none",
                mask: true,
              });
            }
          );
        }, 2000);
      },
      fail: (err) => {
        console.log(err);
      },
      complete: () => {
        setTimeout(() => {
          wx.hideLoading();
        }, 2000);
      },
    });
  },
  // 2.数据的本地存储(同步)
  setLocalData() {
    const token = "qwdwdhkksdhdsjgdsgisdufiussid"
    const person = {
      name: '张三',
      age: 18,
      sex: '男'
    }
    wx.setStorageSync("token", token);
    wx.setStorageSync('person', person)
  },
  getLocalData() {
    console.log(wx.getStorageSync('token'))
    console.log(wx.getStorageSync('person'))
  },
  delLocalData() {
    wx.removeStorageSync('token')
    wx.removeStorageSync('person')
  },
  clearLocalData() {
    wx.clearStorageSync()
  },
  // 2.数据的本地存储(异步)
  setLocalDataAsync() {
    wx.setStorage({
      key: "dog",
      data: {
        name: "旺财",
        age: 10,
        sex: "公"
      },
    });
  },
  getLocalDataAsync() {
    wx.getStorage({
      key: "dog",
      success: (res) => {
        console.log(res.data);
      },
    });
  },
  delLocalDataAsync() {
    wx.removeStorage({
      key: 'dog'
    })
  },
  clearLocalDataAsync() {
    wx.clearStorage()
  },
  // 3. 从本地选择文件
  async uploadImg() {
    // 3.1 写法1
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      success: (res) => {
        this.setData({
          imgUrl: res.tempFiles[0].tempFilePath,
        });
      },
      fail: (res) => {},
      complete: (res) => {},
    });
    // 3.2 写法2
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ["image"],
    // }).then((res) => {
    //   this.setData({
    //     imgUrl: res.tempFiles[0].tempFilePath,
    //   });
    // });

    // 3.3 写法3
    // const res = await wx.chooseMedia({
    //   count: 1,
    //   mediaType: ["image"],
    // });
    // this.setData({
    //   imgUrl: res.tempFiles[0].tempFilePath,
    // });
  },

  // 4.搜索
  doSearch() {
    // 4.1取出数据
    if (!this.data.keyword.trim()) {
      wx.showToast({
        title: "搜索内容不能为空",
        icon: "none",
      });
      return;
    }
    // 4.2把数据放入数组
    const tempArr = this.data.historyArr;
    tempArr.push(this.data.keyword);
    this.setData({
        historyArr: tempArr,
        keyword: "",
      },
      () => {
        wx.setStorageSync('historyArr', this.data.historyArr)
      })
  },
  clearHistory() {
    this.setData({
        historyArr: [],
      },
      () => {
        wx.removeStorageSync('histortArr')
      }
    )
  },
  clickMe(e) {
    console.log("点我了吗", e.target.dataset);
  },
  clickMeTwo(e) {
    console.log(e.mark.name);
  },
});